package com.example.demo.controller;

import com.example.demo.Server.impl.ChatService;
import com.example.demo.entity.ChatMessage;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/api/chat")
public class ChatController {

    @Autowired
    private ChatService chatService;



    @MessageMapping("/chat/{chatId}")
    @SendTo("/topic/chat/{chatId}")
    public ChatMessage sendMessage(ChatMessage message) {
       log.info("Received message: {}", message);
        chatService.saveMessage(message);
        return message;
    }


    @GetMapping("/messages")
    public List<ChatMessage> getChatMessages(@RequestParam String chatId) {
        // 从数据库中获取聊天记录
        List<ChatMessage> byChatId = chatService.findByChatId(chatId);

        return byChatId;
    }
}